<?
FUNCTION crea_route($ter1,$ter2,$ter3,$ter4,$ter5){
         $route=1;
         IF ($ter1==-1 && $ter2==-1 && $ter4==-1 && $ter5==-1){$route=0;}
         ELSEIF ($ter1==-1 && $ter2==-1 && $ter4!=-1 && $ter5==-1){$route=1;}
         ELSEIF ($ter1==-1 && $ter2==-1 && $ter4==-1 && $ter5!=-1){$route=2;}
         ELSEIF ($ter1==-1 && $ter2==-1 && $ter4!=-1 && $ter5!=-1){$route=3;}
         ELSEIF ($ter1==-1 && $ter2!=-1 && $ter4==-1 && $ter5==-1){$route=4;}
         ELSEIF ($ter1==-1 && $ter2!=-1 && $ter4!=-1 && $ter5==-1){$route=5;}
         ELSEIF ($ter1==-1 && $ter2!=-1 && $ter4==-1 && $ter5!=-1){$route=6;}
         ELSEIF ($ter1==-1 && $ter2!=-1 && $ter4!=-1 && $ter5!=-1){$route=7;}
         ELSEIF ($ter1!=-1 && $ter2==-1 && $ter4==-1 && $ter5==-1){$route=8;}
         ELSEIF ($ter1!=-1 && $ter2==-1 && $ter4!=-1 && $ter5==-1){$route=9;}
         ELSEIF ($ter1!=-1 && $ter2==-1 && $ter4==-1 && $ter5!=-1){$route=10;}
         ELSEIF ($ter1!=-1 && $ter2==-1 && $ter4!=-1 && $ter5!=-1){$route=11;}
         ELSEIF ($ter1!=-1 && $ter2!=-1 && $ter4==-1 && $ter5==-1){$route=12;}
         ELSEIF ($ter1!=-1 && $ter2!=-1 && $ter4!=-1 && $ter5==-1){$route=13;}
         ELSEIF ($ter1!=-1 && $ter2!=-1 && $ter4==-1 && $ter5!=-1){$route=14;}
         ELSEIF ($ter1!=-1 && $ter2!=-1 && $ter4!=-1 && $ter5!=-1){$route=15;}
         RETURN $route;
}//Fin de la fonctioncrearoute


FUNCTION crea_route_cote($ter1,$ter2,$ter3,$ter4,$ter5,$ter_x,$ter_y,$terrain_tmp, $terrain_id){

               //Pour savoir quel drap il faut mettre pour savoir d'ou viens la case
             SWITCH ($terrain_tmp){
                 CASE 1: $ter_tmp=$ter5; BREAK;
                 CASE 2: $ter_tmp=$ter4; BREAK;
                 CASE 3: $ter_tmp=$ter3; BREAK;
                 CASE 4: $ter_tmp=$ter2; BREAK;
                 CASE 5: $ter_tmp=$ter1; BREAK;
             }
                       $ter1=-1 ; $ter2=-1 ; $ter4=-1 ; $ter3=-1;  $ter5=-1;
         SWITCH ($ter_tmp){
                  CASE 0: $ter1=-1 ; $ter2=-1 ; $ter4=-1 ; $ter5=-1; BREAK;
                  CASE 1: $ter1=-1 ; $ter2=-1 ; $ter4=1 ; $ter5=-1; BREAK;
                  CASE 2:$ter1=-1 ; $ter2=-1 ; $ter4=-1 ; $ter5=1; BREAK;
                  CASE 3: $ter1=-1 ; $ter2=-1 ; $ter4=1 ; $ter5=1; BREAK;
                  CASE 4: $ter1=-1 ; $ter2=1 ; $ter4=-1 ; $ter5=-1; BREAK;
                  CASE 5: $ter1=-1 ; $ter2=1 ; $ter4=1 ; $ter5=-1; BREAK;
                  CASE 6: $ter1=-1 ; $ter2=1 ; $ter4=-1 ; $ter5=1; BREAK;
                  CASE 7: $ter1=-1 ; $ter2=1 ; $ter4=1 ; $ter5=1; BREAK;
                  CASE 8: $ter1=1 ; $ter2=-1 ; $ter4=0 ; $ter5=-1; BREAK;
                  CASE 9: $ter1=1 ; $ter2=-1 ; $ter4=1 ; $ter5=-1; BREAK;
                  CASE 10: $ter1=1 ; $ter2=-1 ; $ter4=-1 ; $ter5=1; BREAK;
                  CASE 11: $ter1=1 ; $ter2=-1 ; $ter4=1 ; $ter5=1; BREAK;
                  CASE 12: $ter1=1 ; $ter2=1 ; $ter4=-1 ; $ter5=-1; BREAK;
                  CASE 13: $ter1=1 ; $ter2=1 ; $ter4=1 ; $ter5=-1; BREAK;
                  CASE 14: $ter1=1 ; $ter2=1 ; $ter4=-1 ; $ter5=1; BREAK;
                  CASE 15: $ter1=1 ; $ter2=1 ; $ter4=1 ; $ter5=1; BREAK;
             }
             //Pour savoir quel drap il faut mettre pour savoir d'ou viens la case
             SWITCH ($terrain_tmp){
                 CASE 1: $ter1=1; BREAK;
                 CASE 2: $ter2=1; BREAK;
                 CASE 3: $ter3=1; BREAK;
                 CASE 4: $ter4=1; BREAK;
                 CASE 5: $ter5=1; BREAK;
             }


         $ter_route=crea_route($ter1,$ter2,$ter3,$ter4,$ter5);
         echo "</BR>$ter1*$ter2*$ter3*$ter4*$ter5-$ter_route<BR>";
           $query="UPDATE zdaa SET continent_terrain=$terrain_id, continent_style=$ter_route  WHERE continent_x=$ter_x AND continent_y=$ter_y";
           $resultat=mysql_query($query);

           echo $query."</BR>";
         RETURN $route;
}//Fin de la fonctioncrearoute


FUNCTION crea_montagne($terre_x,$terre_y,$terrain_tmp){
    $unit[1]=$terre_x;
    $unit[2]=$terre_y;
                  //  $terrain_tmp=55;
    $x1=$unit[1];$y1=$unit[2];
    $x2=$unit[1]-1;$y2=$unit[2];
    $x3=$unit[1];$y3=$unit[2]-1;
    $x4=$unit[1];$y4=$unit[2]+1;
    $x5=$unit[1]+1;$y5=$unit[2];

  $query="SELECT  continent_x, continent_y,continent_terrain, continent_style FROM zdaa where (continent_x=$x1 AND continent_y=$y1) OR (continent_x=$x2 AND continent_y=$y2) OR (continent_x=$x3 AND continent_y=$y3)  OR (continent_x=$x5 AND continent_y=$y5)  OR(continent_x=$x4 AND continent_y=$y4)GROUP BY  continent_y,continent_x";
  $resultat=mysql_query($query);

             //   echo $query."</BR>";

    $i=0;$terrain[0]=-1;$terrain[1]=-1;$terrain[2]=-1;$terrain[3]=-1;$terrain[4]=-1;$terrain[5]=-1;
    WHILE ($tmp=mysql_fetch_array($resultat)){
               // echo "<br>$tmp[0]-$tmp[1]-$tmp[2]-$tmp[3]";
         IF ($tmp[2]==$terrain_tmp){ $terrain[$i]=$tmp[3];}
         $i++;
    }//Fin du while tmp
                     echo "<br>$terrain[0]/$terrain[1]/$terrain[2]/$terrain[3]/$terrain[4]---</BR>";
          //Creation de la route.
         $ter_route=crea_route($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4]);
            //$ter_route--;     //   echo $ter_route;
           $query="UPDATE zdaa SET continent_terrain=$terrain_tmp, continent_style=$ter_route  WHERE continent_x=$unit[1] AND continent_y=$unit[2]";
           $resultat=mysql_query($query);

                          echo $query;
           //Si il y a une route OUEST
          IF ($terrain[1]!=-1){
                 $tmp_x=$unit[1]-1; $tmp_y=$unit[2];
                 $ter_route=crea_route_cote($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4],$tmp_x,$tmp_y,4,$terrain_tmp);
          }
           //Si il y a une route NORD
          IF ($terrain[0]!=-1){
                 $tmp_x=$unit[1]; $tmp_y=$unit[2]-1;
                 $ter_route=crea_route_cote($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4],$tmp_x,$tmp_y,5,$terrain_tmp);
          }
           //Si il y a une route EST

           IF ($terrain[3]!=-1){
                 $tmp_x=$unit[1]+1; $tmp_y=$unit[2];
                 $ter_route=crea_route_cote($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4],$tmp_x,$tmp_y,2,$terrain_tmp);
          }
          //Si il y a une route SUD
          IF ($terrain[4]!=-1){
                 $tmp_x=$unit[1]; $tmp_y=$unit[2]+1;
                 $ter_route=crea_route_cote($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4],$tmp_x,$tmp_y,1,$terrain_tmp);
          }
}//Fin de  FUNCTION crea_montagne($terre_x,$terre_y){


    /*

FUNCTION crea_route($ter1,$ter2,$ter3,$ter4,$ter5){
         $route=1;
         IF ($ter1==0 && $ter2==0 && $ter4==0 && $ter5==0){$route=0;}
         ELSEIF ($ter1==0 && $ter2==0 && $ter4!=0 && $ter5==0){$route=1;}
         ELSEIF ($ter1==0 && $ter2==0 && $ter4==0 && $ter5!=0){$route=2;}
         ELSEIF ($ter1==0 && $ter2==0 && $ter4!=0 && $ter5!=0){$route=3;}
         ELSEIF ($ter1==0 && $ter2!=0 && $ter4==0 && $ter5==0){$route=4;}
         ELSEIF ($ter1==0 && $ter2!=0 && $ter4!=0 && $ter5==0){$route=5;}
         ELSEIF ($ter1==0 && $ter2!=0 && $ter4==0 && $ter5!=0){$route=6;}
         ELSEIF ($ter1==0 && $ter2!=0 && $ter4!=0 && $ter5!=0){$route=7;}
         ELSEIF ($ter1!=0 && $ter2==0 && $ter4==0 && $ter5==0){$route=8;}
         ELSEIF ($ter1!=0 && $ter2==0 && $ter4!=0 && $ter5==0){$route=9;}
         ELSEIF ($ter1!=0 && $ter2==0 && $ter4==0 && $ter5!=0){$route=10;}
         ELSEIF ($ter1!=0 && $ter2==0 && $ter4!=0 && $ter5!=0){$route=11;}
         ELSEIF ($ter1!=0 && $ter2!=0 && $ter4==0 && $ter5==0){$route=12;}
         ELSEIF ($ter1!=0 && $ter2!=0 && $ter4!=0 && $ter5==0){$route=13;}
         ELSEIF ($ter1!=0 && $ter2!=0 && $ter4==0 && $ter5!=0){$route=14;}
         ELSEIF ($ter1!=0 && $ter2!=0 && $ter4!=0 && $ter5!=0){$route=15;}
         RETURN $route;
}//Fin de la fonctioncrearoute



FUNCTION crea_route_cote($ter1,$ter2,$ter3,$ter4,$ter5,$ter_x,$ter_y,$terrain_tmp,$terre_id){
            //Pour savoir quel drap il faut mettre pour savoir d'ou viens la case
             SWITCH ($terrain_tmp){
                 CASE 1: $ter_tmp=$ter5; BREAK;
                 CASE 2: $ter_tmp=$ter4; BREAK;
                 CASE 3: $ter_tmp=$ter3; BREAK;
                 CASE 4: $ter_tmp=$ter2; BREAK;
                 CASE 5: $ter_tmp=$ter1; BREAK;
             }

         SWITCH ($ter_tmp){
                  CASE 0: $ter1=0 ; $ter2=0 ; $ter4=0 ; $ter5=0; BREAK;
                  CASE 1: $ter1=0 ; $ter2=0 ; $ter4=1 ; $ter5=0; BREAK;
                  CASE 2:$ter1=0 ; $ter2=0 ; $ter4=0 ; $ter5=1; BREAK;
                  CASE 3: $ter1=0 ; $ter2=0 ; $ter4=1 ; $ter5=1; BREAK;
                  CASE 4: $ter1=0 ; $ter2=1 ; $ter4=0 ; $ter5=0; BREAK;
                  CASE 5: $ter1=0 ; $ter2=1 ; $ter4=1 ; $ter5=0; BREAK;
                  CASE 6: $ter1=0 ; $ter2=1 ; $ter4=0 ; $ter5=1; BREAK;
                  CASE 7: $ter1=0 ; $ter2=1 ; $ter4=1 ; $ter5=1; BREAK;
                  CASE 8: $ter1=1 ; $ter2=0 ; $ter4=0 ; $ter5=0; BREAK;
                  CASE 9: $ter1=1 ; $ter2=0 ; $ter4=1 ; $ter5=0; BREAK;
                  CASE 10: $ter1=1 ; $ter2=0 ; $ter4=0 ; $ter5=1; BREAK;
                  CASE 11: $ter1=1 ; $ter2=0 ; $ter4=1 ; $ter5=1; BREAK;
                  CASE 12: $ter1=1 ; $ter2=1 ; $ter4=0 ; $ter5=0; BREAK;
                  CASE 13: $ter1=1 ; $ter2=1 ; $ter4=1 ; $ter5=0; BREAK;
                  CASE 14: $ter1=1 ; $ter2=1 ; $ter4=0 ; $ter5=1; BREAK;
                  CASE 15: $ter1=1 ; $ter2=1 ; $ter4=1 ; $ter5=1; BREAK;
             }

             //Pour savoir quel drap il faut mettre pour savoir d'ou viens la case
             SWITCH ($terrain_tmp){
                 CASE 1: $ter1=1; BREAK;
                 CASE 2: $ter2=1; BREAK;
                 CASE 3: $ter3=1; BREAK;
                 CASE 4: $ter4=1; BREAK;
                 CASE 5: $ter5=1; BREAK;
             }


         $ter_route=crea_route($ter1,$ter2,$ter3,$ter4,$ter5);

        echo "</BR>$ter1*$ter2*$ter3*$ter4*$ter5-$ter_tmp--$terrain_tmp";
          // $ter_route++;
             echo "***".$ter_route."***<BR>";
           $query="UPDATE zdaa SET continent_terrain=$terre_id, continent_style=$ter_route  WHERE continent_x=$ter_x AND continent_y=$ter_y";
           $resultat=mysql_query($query);

               //   echo $query."</BR>";
         RETURN $route;
}//Fin de la fonctioncrearoute



FUNCTION crea_montagne($terre_x,$terre_y,$terrain_tmp){
    $unit[1]=$terre_x;
    $unit[2]=$terre_y;

    $x1=$unit[1];$y1=$unit[2];
    $x2=$unit[1]-1;$y2=$unit[2];
    $x3=$unit[1];$y3=$unit[2]-1;
    $x4=$unit[1];$y4=$unit[2]+1;
    $x5=$unit[1]+1;$y5=$unit[2];

  $query="SELECT  continent_x, continent_y,continent_terrain, continent_style FROM zdaa where (continent_x=$x1 AND continent_y=$y1) OR (continent_x=$x2 AND continent_y=$y2) OR (continent_x=$x3 AND continent_y=$y3)  OR (continent_x=$x5 AND continent_y=$y5)  OR(continent_x=$x4 AND continent_y=$y4)GROUP BY  continent_y,continent_x";
  $resultat=mysql_query($query);

               // echo $query."</BR>";

     $i=0;$terrain[0]=0;$terrain[1]=0;$terrain[2]=0;$terrain[3]=0;$terrain[4]=0;$terrain[5]=0;
    WHILE ($tmp=mysql_fetch_array($resultat)){
               // echo "<br>$tmp[0]-$tmp[1]-$tmp[2]-$tmp[3]";
         IF ($tmp[2]==$terrain_tmp){$terrain[$i]=$tmp[2];}
         $i++;
    }//Fin du while tmp
                     echo "<br>$terrain[0]-$terrain[1]-$terrain[2]-$terrain[3]-$terrain[4]</BR>";
          //Creation de la route.
            $ter_route=crea_route($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4]);
          // $ter_route--;
             echo "***".$ter_route."***<BR>";
           $query="UPDATE zdaa SET continent_terrain=$terrain_tmp, continent_style=$ter_route  WHERE continent_x=$unit[1] AND continent_y=$unit[2]";
           $resultat=mysql_query($query);

                            echo $query;
          //Si il y a une route OUEST
          IF ($terrain[1]!=0){
                 $tmp_x=$unit[1]-1; $tmp_y=$unit[2];
                 $ter_route=crea_route_cote($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4],$tmp_x,$tmp_y,4,$terrain_tmp);
          }
           //Si il y a une route NORD
          IF ($terrain[0]!=0){
                 $tmp_x=$unit[1]; $tmp_y=$unit[2]-1;
                 $ter_route=crea_route_cote($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4],$tmp_x,$tmp_y,5,$terrain_tmp);
          }
           //Si il y a une route EST

           IF ($terrain[3]!=0){
                 $tmp_x=$unit[1]+1; $tmp_y=$unit[2];
                 $ter_route=crea_route_cote($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4],$tmp_x,$tmp_y,2,$terrain_tmp);
          }
          //Si il y a une route SUD
          IF ($terrain[4]!=0){
                 $tmp_x=$unit[1]; $tmp_y=$unit[2]+1;
                 $ter_route=crea_route_cote($terrain[0],$terrain[1],$terrain[2],$terrain[3],$terrain[4],$tmp_x,$tmp_y,1,$terrain_tmp);
          }

}//Fin de  FUNCTION crea_montagne($terre_x,$terre_y){

      */
?>
